<?php
/**
 * @desc	: File dung chung cho mot so cac ham co ban thao tac co so du lieu.
 * @coder	: Nguyen Luong Dinh
 *
 */
class Mod_Lib extends CI_Model
{
	function Mod_Lib()
	{
		parent::__construct();
	}
	
	function insert($table,$data) {
		if($this->db->insert($table,$data)) {
			return true;
		} else {
			return false;
		}
	}
	
	function join($table,$select,$where,$join=array(),$limit,$offset,$order){
		if (isset($select) && !empty($select))
		{
			$this->db->select("$select");
		}
		if (isset($where) && !empty($where))
		{
			foreach ($where as $key => $value) {
				$this->db->where($key,$value);
			}
		}
		if (isset($join) && !empty($join))
		{
			foreach ($join as $key => $value) {
				$this->db->join($key,$value);
			}
		}
		if (isset($limit) && !empty($limit))
		{
			if (isset($offset) && !empty($offset))
			{
				$this->db->limit($limit,$offset);
			}else{
				$this->db->limit($limit);
			}
		}
		if (isset($order) && !empty($order))
		{
			foreach ($order as $key=>$value)
			{
				$this->db->order_by($key,$value);
			}
		}
		return $this->db->get("$table")->result_array();
	}
	
	
	function join_like($table,$select,$like,$where,$join=array(),$limit,$offset,$order){
		if (isset($select) && !empty($select))
		{
			$this->db->select("$select");
		}
		if(isset($like) && !empty($like)){
			foreach ($like as $key => $value){
				$this->db->like($key,$value);
			}
		}
		if (isset($where) && !empty($where))
		{
			foreach ($where as $key => $value) {
				$this->db->where($key,$value);
			}
		}
		if (isset($join) && !empty($join))
		{
			foreach ($join as $key => $value) {
				$this->db->join($key,$value);
			}
		}
		if (isset($limit) && !empty($limit))
		{
			if (isset($offset) && !empty($offset))
			{
				$this->db->limit($limit,$offset);
			}else{
				$this->db->limit($limit);
			}
		}
		if (isset($order) && !empty($order))
		{
			foreach ($order as $key=>$value)
			{
				$this->db->order_by($key,$value);
			}
		}
		return $this->db->get("$table")->result_array();
	}
	
	/*
	 * function load_all
	 * @desc: lay ra cac record theo cac dieu kieu phan trang, sap xep, tim kiem
	 * @table: ten bang - string
	 * @select: cac truong can lay ra - string
	 * @where: cac dieu kien - array()
	 * @prepage: so record tren mot trang - number
	 * @uri: uri paging - number 
	 * @order: - array()
	 */
	function load_all($table,$select,$where,$perpage,$uri,$order){
		if(isset($select) && !empty($select)){
			$this->db->select($select);
		}
		
		if(isset($where) && !empty($where)){
			foreach ($where as $key => $value) {
				$this->db->where($key,$value);
			}
		}

		if (isset($perpage) && !empty($perpage)){
			if (isset($uri) && !empty($uri))
			{
				$this->db->limit($perpage,$uri);
			} else {
				$this->db->limit($perpage);
			}
		}
		
		if(isset($order) && !empty($order)){
			foreach ($order as $key=>$value)
			{
				$this->db->order_by($key,$value);
			}
		}
		
		return $this->db->get($table)->result_array();
	}
	
	function load_all_product($table,$select,$where,$perpage,$uri,$order,$join){
		if(isset($select) && !empty($select)){
			$this->db->select($select);
		}
		
		if(isset($where) && !empty($where)){
			foreach ($where as $key => $value) {
				$this->db->where($key,$value);
			}
		}

		if (isset($perpage) && !empty($perpage)){
			if (isset($uri) && !empty($uri))
			{
				$this->db->limit($perpage,$uri);
			} else {
				$this->db->limit($perpage);
			}
		}
		
		if(isset($order) && !empty($order)){
			foreach ($order as $key=>$value)
			{
				$this->db->order_by($key,$value);
			}
		}
		
		return $this->db->get($table)->result_array();
	}
	/*
	 * function load_search
	 * @desc: lay ra cac record theo cac dieu kieu phan trang, sap xep, tim kiem
	 * @table: ten bang - string
	 * @like: dieu kien tim kiem
	 * @select: cac truong can lay ra - string
	 * @where: cac dieu kien - array()
	 * @prepage: so record tren mot trang - number
	 * @uri: uri paging - number 
	 * @order: - array()
	 */
	function load_search($table,$select,$like,$where,$perpage,$uri,$order){
		if(isset($select) && !empty($select)){
			$this->db->select($select);
		}
		if(isset($like) && !empty($like)){
			foreach ($like as $key => $value){
				$this->db->like($key,$value);
			}
		}
		if(isset($where) && !empty($where)){
			foreach ($where as $key => $value) {
				$this->db->where($key,$value);
			}
		}
		if (isset($perpage) && !empty($perpage)){
			if (isset($uri) && !empty($uri))
			{
				$this->db->limit($perpage,$uri);
			} else {
				$this->db->limit($perpage);
			}
		}
		
		if(isset($order) && !empty($order)){
			foreach ($order as $key=>$value)
			{
				$this->db->order_by($key,$value);
			}
		}
		
		return $this->db->get($table)->result_array();
	}
	
	/*
	 * function permis_menu_admin
	 * @desc: lay ra cac quyen tuong ung voi cac admin
	 */
	function permis_menu_admin($id){
		/*
		$sql =  "SELECT DISTINCT pa.id as permis_admin_id,pa.access,pa.additional,pa.edit,pa.del,ma.id as menu_admin_id, ma.name 
				 FROM aha_permis_admin pa, aha_admin_menus ma 
				 WHERE pa.admin_id = '$id' 
				 AND ma.id = pa.menu_admin_id 
				 AND cat_id !=0 
				 ORDER BY ma.id ASC";
		*/
		
		$sql =  "SELECT permiss_admins.*, admin_menu.controller, admin_menu.id AS id_menu 
				 FROM  permiss_admins, admin_menu  
				 WHERE permiss_admins.admins_id = '$id' 
				 AND   admin_menu.id = permiss_admins.admin_menu_id 
				 ";
		
		return $this->db->query($sql)->result_array();
	}
	
	/*
	 * function search
	 * @desc: tim kiem
	 * @table: bang du lieu
	 * @select: cac truong du lieu can select
	 * @like: dieu kien tim kien like
	 * @where: dieu kien tim kien where
	 * @order: sap xep
	 */
	function search($table,$select,$like,$where,$order){
		if(isset($select) && !empty($select)){
			$this->db->select($select);
		}
		if(isset($like) && !empty($like)){
			foreach ($like as $key => $value){
				$this->db->like($key,$value);
			}
		}
		if(isset($where) && !empty($where)){
			foreach ($where as $key => $value) {
				$this->db->where($key,$value);
			}
		}
		if(isset($order) && !empty($order)){
			foreach ($order as $key=>$value)
			{
				$this->db->order_by($key,$value);
			}
		}
		return $this->db->get($table)->result_array();	
	}
	
	/*
	 * function insert_log
	 * @desc: luu cac tac vu
	 * @action: tac vu
	 * @admi_id: id admin 
	 */
	function insert_log($action,$admin_id){		
		$data	=array(
			'action' 			=> $action,
			'time'				=>time(),
			'ip'				=>$this->input->ip_address(),
			'admin_id'			=>$admin_id
		);
		
		if($this->db->insert('admin_log',$data)){
			return true;
		} else {
			return false;
		}
	}
	
	/*
	 * function - deleteRecord
	 * @desc: xoa mot hoạc nhieu ban ghi
	 * @table: tên bảng
	 * @id: tên id để dạng chuỗi (1,2,3,4,5,6)
	 */
	function deleteRecord($table,$id) {
		$sql 	 	= 'DELETE FROM ' .$table. ' WHERE id IN ('.$id.')';	 
		$this->db->query($sql);
	}
	
	function deleteRecordCategory($table,$cat_id){
		$ctg = $this->db->get('admin_menu')->result_array();
			
		foreach ($cat_id as $key=>$value) {
			$this->db->where('id',$value);
			$cate = $this->db->get($table)->row_array();
			
			if ($cate['level'] == 1) {
				$this->db->where('parent_id',$cate['id']);
				$cate2 = $this->db->get($table)->result_array();
					
				foreach ($cate2 as $category){
					$this->db->where('root_patch',$category['id']);
					$this->db->delete($table);
					$this->db->where('id',$category['id']); 
					$this->db->delete($table);		
				}
				
				$this->db->where('id',$cate['id']); 
				$this->db->delete($table);
				
			} else if ($cate['level'] == 2) {
				$this->db->where('root_patch',$cate['id']);
				$this->db->delete($table);
				
				$this->db->where('admin_menu_id',$cate['id']);
				$this->db->delete('permiss_admins');
				
				$this->db->where('id',$cate['id']); 
				$this->db->delete($table);	
					
			} else if ($cate['level'] == 3) {
				$this->db->where('admin_menu_id',$cate['id']);
				$this->db->delete('permiss_admins');
				
				$this->db->where('id',$cate['id']);
				$this->db->delete($table);
			}
		}
	}
	
	function deleteCategory($table,$cat_id){
		$ctg = $this->db->get($table)->result_array();
		if(is_array($cat_id)) {
			foreach ($cat_id as $key=>$value) {
				$this->db->where('id',$value);
				$cate = $this->db->get($table)->row_array();
				
				if ($cate['level'] == 1) {
					$this->db->where('parent_id',$cate['id']);
					$cate2 = $this->db->get($table)->result_array();
						
					foreach ($cate2 as $category){
						$this->db->where('parent_id',$category['id']);
						$this->db->delete($table);
						$this->db->where('id',$category['id']); 
						$this->db->delete($table);		
					}
					
					$this->db->where('id',$cate['id']); 
					$this->db->delete($table);
					
				} else if ($cate['level'] == 2) {
					$this->db->where('parent_id',$cate['id']);
					$this->db->delete($table);
					$this->db->where('id',$cate['id']); 
					$this->db->delete($table);	
						
				} else if ($cate['level'] == 3) {
					$this->db->where('id',$cate['id']);
					$this->db->delete($table);
				}
			}
		} else {
				$this->db->where('id',$cat_id);
				$cate = $this->db->get($table)->row_array();
				
				
				if ($cate['level'] == 1) {
					$this->db->where('parent_id',$cate['id']);
					$cate2 = $this->db->get($table)->result_array();
						
					foreach ($cate2 as $category){
						$this->db->where('parent_id',$category['id']);
						$this->db->delete($table);
						$this->db->where('id',$category['id']); 
						$this->db->delete($table);		
					}
					
					$this->db->where('id',$cate['id']); 
					$this->db->delete($table);
					
				} else if ($cate['level'] == 2) {
					$this->db->where('parent_id',$cate['id']);
					$this->db->delete($table);
					$this->db->where('id',$cate['id']); 
					$this->db->delete($table);	
						
				} else if ($cate['level'] == 3) {
					$this->db->where('id',$cate['id']);
					$this->db->delete($table);
				}
		}	
	}
	
	/*
	 * function - update
	 * @desc: function update (dung cho viec cap nhat du lieu va active trang thai)
	 * Các tham số: 
	 * $table: tên bảng
	 * $where: Mảng các điều kiện
	 * $order: Sap xep
	 * $data: Mảng chứa dữ liệu cần update
	 */
	function update($table,$where=array(),$order=array(),$data)
	{
		foreach ($where as $key=>$value){
			$this->db->where($key,$value);
		}

		foreach ($order as $key=>$value) {
			$this->db->order_by($key,$value);
		}
		
		if($this->db->update($table,$data)){
			return true;
		} else {
			return false;
		}
	}
	
	
	
	/*
	 * function - findOrderList
	 * @desc: dung cho viec lay ra cac danh muc menu admin hoac danh muc hien thi trong trang chu
	 * Các tham số:
	 */
	
	function  findOrderList($table) {
		//tim tat ca cac danh muc
		$rt = array(); 
		$this->db->order_by('root_path','asc');
		$categories = $this->db->get($table)->result_array();
		
		//cong chuoi '-' vao cho tung danh muc theo tung level
		foreach ($categories as $category) {
			$ctg_str = "";
			$spaceAlowlevel = str_repeat('- ', $category['level'] - 1);
			$ctg_str .= $spaceAlowlevel.$category['name'];
			$rt[$category['id']] = $ctg_str;  
		}
		
		return $rt;
	}
	
	
	function  findOrderListCategory($table) {
		//tim tat ca cac danh muc
		$rt = array(); 
		$this->db->where('level <','5');
		$this->db->order_by('root_path','asc');
		$categories = $this->db->get($table)->result_array();

			//cong chuoi '-' vao cho tung danh muc theo tung level
			foreach ($categories as $category) {
				$ctg_str = "";
				$spaceAlowlevel = str_repeat('- ', $category['level'] - 1);
				$ctg_str .= $spaceAlowlevel.$category['name'];
				$rt[$category['id']] = $ctg_str;  
			}
		return $rt;
	}
	
	function  findOrderListCategory_Prd($table) {
		//tim tat ca cac danh muc
		$rt = array(); 
		$this->db->where('level <','4');
		$this->db->order_by('root_path','asc');
		$categories = $this->db->get($table)->result_array();

			//cong chuoi '-' vao cho tung danh muc theo tung level
			foreach ($categories as $category) {
				$ctg_str = "";
				$spaceAlowlevel = str_repeat('- ', $category['level'] - 1);
				$ctg_str .= $spaceAlowlevel.$category['name'];
				$rt[$category['id']] = $ctg_str;  
			}
		return $rt;
	}
	
	/* 
	 * function - findJoin
	 * @desc: tim cac bang co quan he voi nhau
	 * 
	 /
	 */
}
?>